package cn.pconline.autoclub.web.app;

import cn.pconline.autoclub.config.SystemConfig;
import cn.pconline.autoclub.entity.Activity;
import cn.pconline.autoclub.entity.Album;
import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubDyna;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.DynaImage;
import cn.pconline.autoclub.entity.DynaIndex;
import cn.pconline.autoclub.entity.DynaInfo;
import cn.pconline.autoclub.entity.DynaPraise;
import cn.pconline.autoclub.entity.Subject;
import cn.pconline.autoclub.entity.SubjectDyna;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.entity.UserDyna;
import cn.pconline.autoclub.entity.UserPraise;
import cn.pconline.autoclub.entity.Vote;
import cn.pconline.autoclub.service.ActivityService;
import cn.pconline.autoclub.service.AlbumService;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.service.DynaIndexService;
import cn.pconline.autoclub.service.EffectiveDynaService;
import cn.pconline.autoclub.service.MemberService;
import cn.pconline.autoclub.service.MessageService;
import cn.pconline.autoclub.service.SubjectDynaService;
import cn.pconline.autoclub.service.SubjectService;
import cn.pconline.autoclub.service.UserService;
import cn.pconline.autoclub.service.VoteService;
import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.StringUtil;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.util.app.AppControllerStatic;
import cn.pconline.autoclub.web.UserAuth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.wordnik.swagger.annotations.Api;
//import com.wordnik.swagger.annotations.ApiOperation;


import com.wordnik.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.gelivable.dao.GeliDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
 * @author zhouruxuan
 * @date 2015年2月4日 上午11:35:11
 */
@Configuration
@EnableSwagger
@Controller
@RequestMapping(value = "/appapi")

@Api(value = "/helloWorld", description = "Greeting API", position = 1) 
public class AppDynaController {
    @Autowired
    private EffectiveDynaService dynaService;
    @Autowired
    private UserService userService;
    @Autowired
    private ClubService clubService;
    @Autowired
    private SystemConfig systemConfig;
    @Autowired
    private AlbumService albumService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private VoteService voteService;
    @Autowired
    private UserAuth userAuth;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SubjectDynaService subjectDynaService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private GeliDao geliDao;
    @Autowired
    private DynaIndexService dynaIndexService;
    
    private static final Logger log = LoggerFactory.getLogger(AppDynaController.class);
    private static final int MAX_TOP_COUNT = 5;//最大置顶数
    private static final int ORDER_POST_DATE = 1;//回复时间排序
    private static final int ORDER_CREATE_DATE = 2;//发布时间排序

    /**
     * 获取单条动态详情 请求参数：dynaInfoId createDate
     *
     * @param request
     * @param response
     */
    
    @ApiOperation(value = "修改学生信息", httpMethod = "POST", notes = "学生信息以json格式传递", response = DynaInfo.class)
    @RequestMapping(value = "/1.0/dyna/getDynaDetail.do", method = RequestMethod.GET)
    @ResponseBody
    public void getDynaDetail(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);
        Date createDate = WebUtils.parseDate(request, "createDate", null);
        JSONObject json = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //参数检查
        if (dynaInfoId == 0 || createDate == null) {
            AppControllerStatic.printErrorMsg(response, "请输入合法的参数：dynaInfoId,createDate", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //获取动态信息
        DynaInfo dynaInfo = dynaService.findDynaById(dynaInfoId, createDate);
        if (dynaInfo == null) {
            AppControllerStatic.printErrorMsg(response, "动态不存在！", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;

        }
        JSONObject jsonObject = dynaService.toJson4App(dynaInfo, xssEncode);
        //返回数据
        jsonArray.add(jsonObject);
        json.put("dynaDetail", jsonArray);
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 获取单条动态详情 请求参数：dynaInfoId createBy
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/2.0/dyna/getDynaDetail.do", method = RequestMethod.GET)
    @ResponseBody
    public void getDynaDetail2(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);
        long createBy = WebUtils.paramLong(request, "createBy", 0L);
        JSONObject json = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //获取动态信息
        UserDyna userDyna = dynaService.findUserDynaById(dynaInfoId, createBy);
        if (userDyna == null || userDyna.getStatus() == UserDyna.STATUS_DELETE) {
            AppControllerStatic.printErrorMsg(response, "动态不存在！", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        DynaInfo dynaInfo = dynaService.findDynaById(dynaInfoId, userDyna.getCreateDate());
        if (dynaInfo == null) {
            AppControllerStatic.printErrorMsg(response, "动态不存在！", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;

        }
        JSONObject jsonObject = dynaService.toJson4App(dynaInfo, xssEncode);
        //返回数据
        jsonArray.add(jsonObject);
        json.put("dynaDetail", jsonArray);
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }
    
    /**
     * 获取单条动态详情 请求参数：dynaInfoId
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/3.0/dyna/getDynaDetail.do", method = RequestMethod.GET)
    @ResponseBody
    public void getDynaDetail3(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);
        JSONObject json = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //获取动态信息
        DynaIndex dynaIndex = dynaIndexService.findDynaIndex(dynaInfoId);
        if (dynaIndex == null || dynaIndex.getStatus() == DynaIndex.STATUS_DELETE) {
            AppControllerStatic.printErrorMsg(response, "动态不存在！", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        DynaInfo dynaInfo = dynaService.findDynaById(dynaInfoId, dynaIndex.getCreateDate());
        if (dynaInfo == null) {
            AppControllerStatic.printErrorMsg(response, "动态不存在！", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;

        }
        JSONObject jsonObject = dynaService.toJson4App(dynaInfo, xssEncode);
        //返回数据
        jsonArray.add(jsonObject);
        json.put("dynaDetail", jsonArray);
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }
    
    /**
     * 根据用户ID获取此用户的动态列表
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getDynaListByUserId.do", method = RequestMethod.GET)
    public void getDynaListByUserId(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long userId = AppControllerStatic.paramUserId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);


        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();

        //用户参数检查
        if (userId == 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数userId", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //用户合法检查
        User user = userService.findUser(userId);
        if (user == null) {
            AppControllerStatic.printErrorMsg(response, "此用户不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        jsonObject.put("userId", user.getUid());

        //用户个人动态列表
        Pager<DynaInfo> pager = dynaService.getDynaListByUserId(userId, pageNo, pageSize);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                //不返回品牌车友会活动动态
                if (dynaInfo.getType() == DynaInfo.TYPE_JOIN_ACTIVITY) {
                    continue;
                }
                JSONObject json = dynaService.toJson4App(dynaInfo, xssEncode);
                jsonArray.add(json);
            }
        }

        //返回Json
        jsonObject.put("dynaList", jsonArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 根据车友会ID获取车友会动态列表
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getDynaListImages.do", method = RequestMethod.GET)
    public void getDynaListImages(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long clubId = AppControllerStatic.paramClubId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //参数检查
        if (clubId <= 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数clubId", jsonObject, callback, AppControllerStatic.V1_VERSION);
            return;
        }
        //车友会动态列表
        Pager<DynaInfo> pager = dynaService.getDynaListByClubId(clubId, pageNo, pageSize);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                //品牌车友会活动动态屏蔽
                if (dynaInfo.getType() == DynaInfo.TYPE_JOIN_ACTIVITY) {
                    continue;
                }
                JSONObject json = dynaService.toJson4App(dynaInfo, xssEncode);
                jsonArray.add(json);
            }
        }
        jsonObject.put("dynaList", jsonArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V1_VERSION);
    }

    /**
     * 根据车友会ID获取车友会动态列表 不同的动态类型返回不同的字段
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/2.0/dyna/getDynaListImages.do", method = RequestMethod.GET)
    public void getDynaListImages2(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long clubId = AppControllerStatic.paramClubId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //车友会动态列表
        Pager<DynaInfo> pager = dynaService.getDynaListByClubId(clubId, pageNo, pageSize);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                //品牌车友会活动动态屏蔽
                if (dynaInfo.getType() == DynaInfo.TYPE_JOIN_ACTIVITY) {
                    continue;
                }
                JSONObject json = dynaService.toJson4App2(dynaInfo, xssEncode);
                jsonArray.add(json);
            }
        }
        jsonObject.put("dynaList", jsonArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V2_VERSION_LATEST);
    }
    
    /**
     * 获取同城动态列表
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getTongChengDynaList.do", method = RequestMethod.GET)
    public void getTongChengDynaList(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        WebUtils.setCacheHeader(response, 900);//缓存15分钟 同城动态，访问比较少，可以缓存下
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long provinceId = AppControllerStatic.paramProvinceId(request);
        long cityId = AppControllerStatic.paramCityId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //参数检查
        if (provinceId <= 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数provinceId", jsonObject, callback, AppControllerStatic.V1_VERSION);
            return;
        }
        //同城动态列表,(只返回说说，帖子，提问帖子）
        List<Integer> typeList = new ArrayList<Integer>();
        typeList.add(DynaInfo.TYPE_TOPIC);
        typeList.add(DynaInfo.TYPE_COMMENT);
        typeList.add(DynaInfo.TYPE_QUESTION);
        Pager<DynaInfo> pager = dynaService.getDynaListByCityIdDynaType(provinceId, cityId, typeList, pageNo, pageSize);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                JSONObject json = dynaService.toJson4App(dynaInfo, xssEncode);
                jsonArray.add(json);
            }
        }
        jsonObject.put("dynaList", jsonArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V1_VERSION);
    }

    /**
     * 获取同城动态列表
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/2.0/dyna/getTongChengDynaList.do", method = RequestMethod.GET)
    public void getTongChengDynaList2(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long provinceId = AppControllerStatic.paramProvinceId(request);
        long cityId = AppControllerStatic.paramCityId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //参数检查
        if (provinceId == 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数provinceId", jsonObject, callback, AppControllerStatic.V2_VERSION);
            return;
        }
        //同城动态列表,(只返回说说，帖子，提问帖子）
        List<Integer> typeList = new ArrayList<Integer>();
        typeList.add(DynaInfo.TYPE_TOPIC);
        typeList.add(DynaInfo.TYPE_COMMENT);
        typeList.add(DynaInfo.TYPE_QUESTION);
        Pager<DynaInfo> pager = dynaService.getDynaListByCityIdDynaType(provinceId, cityId, typeList, pageNo, pageSize);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                JSONObject json = dynaService.toJson4App2(dynaInfo, xssEncode);
                jsonArray.add(json);
            }
        }
        jsonObject.put("dynaList", jsonArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V2_VERSION);
    }
    
    /**
     * 获取同城动态列表
     * 增加：1.帖子时，增加帖子所有的车友圈链接
     *       2.说说时，增加所有车友会的链接
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/3.0/dyna/getTongChengDynaList.do", method = RequestMethod.GET)
    public void getTongChengDynaList3(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        long provinceId = AppControllerStatic.paramProvinceId(request);
        long cityId = AppControllerStatic.paramCityId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        //参数检查
        if (provinceId == 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数provinceId", jsonObject, callback, AppControllerStatic.V3_VERSION_LATEST);
            return;
        }
        //同城动态列表,(只返回说说，帖子，提问帖子）
        List<Integer> typeList = new ArrayList<Integer>();
        typeList.add(DynaInfo.TYPE_TOPIC);
        typeList.add(DynaInfo.TYPE_COMMENT);
        typeList.add(DynaInfo.TYPE_QUESTION);
        Pager<DynaInfo> pager = dynaService.getDynaListByCityIdDynaType(provinceId, cityId, typeList, pageNo, pageSize);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                JSONObject json = dynaService.toJson4App3(dynaInfo, xssEncode);
                jsonArray.add(json);
            }
        }
        jsonObject.put("dynaList", jsonArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V3_VERSION_LATEST);
    }

    /**
     * 1.1.26 点赞/取消赞
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/doPraise.do", method = RequestMethod.POST)
    public void doPraise(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        JSONArray praiseDynaInfoIds = WebUtils.parseArray(request, "praiseDynaInfoIds");
        JSONArray unPraiseDynaInfoIds = WebUtils.parseArray(request, "unPraiseDynaInfoIds");
        JSONObject jsonObject = new JSONObject();
        //获取用户登录信息
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        if (praiseDynaInfoIds == null && unPraiseDynaInfoIds == null) {
            AppControllerStatic.printErrorMsg(response, "参数有误", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        JSONArray failPraiseIds = new JSONArray();
        if (praiseDynaInfoIds != null) {
            for (int i = 0; i < praiseDynaInfoIds.size(); i++) {
                JSONObject obj = null;
                //判断是否转换成功
                try {
                    obj = praiseDynaInfoIds.getJSONObject(i);
                    if (!dynaService.doPraise(user.getUid(), obj.getLong("dynaInfoId"), obj.getDate("createDate"), DynaPraise.STATUS_PRAISE)) {
                        failPraiseIds.add(obj);
                    }
                } catch (Exception exception) {
                    log.error("[praiseDynaInfoIds] fail to get dynaInfo, data={}", obj);
                    AppControllerStatic.printErrorMsg(response, "参数有误", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
                    return;
                }
            }
        }
        JSONArray failNotPraiseIds = new JSONArray();
        if (unPraiseDynaInfoIds != null) {
            for (int i = 0; i < unPraiseDynaInfoIds.size(); i++) {
                JSONObject obj = null;
                //判断是否转换成功
                try {
                    obj = unPraiseDynaInfoIds.getJSONObject(i);
                    if (!dynaService.doPraise(user.getUid(), obj.getLong("dynaInfoId"), obj.getDate("createDate"), DynaPraise.STATUS_UNPRAISE)) {
                        failNotPraiseIds.add(obj);
                    }
                } catch (Exception e) {
                    log.error("[unPraiseDynaInfoIds] fail to get dynaInfo, data={}", obj);
                    AppControllerStatic.printErrorMsg(response, "参数有误", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
                    return;
                }
            }
        }
        jsonObject.put("time", System.currentTimeMillis());
        if (failPraiseIds.isEmpty() && failNotPraiseIds.isEmpty()) {
            AppControllerStatic.printSuccessMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
        } else {
            jsonObject.put("failPraiseDynaInfoIds", failPraiseIds);
            jsonObject.put("failUnPraiseDynaInfoIds", failNotPraiseIds);
            AppControllerStatic.printErrorMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
        }
    }

    /**
     * 1.1.27 收到的赞列表
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getPraiseList.do", method = RequestMethod.GET)
    public void getPraiseList(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        //获取用户登录信息
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "用户末登录", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        int qua = systemConfig.getImageQua();
        Pager<UserPraise> pager = dynaService.getPraiseList(user.getUid(), pageNo, pageSize);
        JSONArray arr = new JSONArray();
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (UserPraise si : pager.getResultList()) {
                DynaInfo dyna = dynaService.findDynaById(si.getDynainfoId(), si.getDynainfoCreateAt());
                //品牌车友会活动动态屏蔽
                if (dyna != null && dyna.getType() == DynaInfo.TYPE_JOIN_ACTIVITY) {
                    continue;
                }
                User u = userService.findUser(si.getUserId());
                JSONObject obj = new JSONObject();
                obj.put("userId", u.getUid());
                obj.put("nickName", u.getNickname());
                obj.put("isVip", u.getVip());
                obj.put("praiseTime", si.getPraiseTime());
                obj.put("hadRead", si.getIsRead());
                if (dyna != null) {
                    obj.put("dynaType", dyna.getType());
                    obj.put("dynaInfoId", dyna.getDynaInfoId());
                    obj.put("createDate", DateUtils.format(dyna.getCreateDate(), "yyyy-MM-dd"));
                    obj.put("createBy", dyna.getCreateBy());
                    obj.put("title", dyna.getTitle());
                    JSONArray a = new JSONArray();
                    if (dyna.getImages() != null) {
                        for (DynaImage i : dyna.getImages()) {
                            JSONObject o = new JSONObject();
                            o.put("url", i.getUrl());
                            o.put("width", i.getWidth());
                            o.put("height", i.getHeight());
                            o.put("qua", qua);
                            a.add(o);
                        }
                    }
                    obj.put("images", a);
                    //根据不同动态类型，返回不同的动态内容
                    obj = dynaService.setSpecialDynaByType4App(obj, dyna, dyna.getType());
                }

                arr.add(obj);
            }
        }
        jsonObject.put("praiseList", arr);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, null, pager, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 1.1.28 未读点赞清零 如果参数dynaInfoIds为空值，清除登录用户全部未读赞
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/clearUnreadPraise.do", method = RequestMethod.POST)
    public void clearUnreadPraise(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String[] dynainfoIds = WebUtils.param(request, "dynaInfoIds", "").split(",");
        JSONObject jsonObject = new JSONObject();
        //获取用户登录信息
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "用户末登录", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        List<Long> ids = new ArrayList<Long>(dynainfoIds.length);
        for (String id : dynainfoIds) {
            try {
                ids.add(Long.parseLong(id));
            } catch (java.lang.NumberFormatException e) {
                continue;
            }
        }
        //已赞清零
        List<Long> rs = dynaService.clearUnreadPraise(user.getUid(), ids);
        //去掉重复的动态ID
        HashSet<Long> set = new HashSet<Long>(rs);
        rs.clear();
        rs.addAll(set);

        jsonObject.put("successIds", StringUtil.toString(rs.toArray()));
        AppControllerStatic.printSuccessMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 1.1.29 获取动态点赞用户列表
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getPraiseUserList.do", method = RequestMethod.GET)
    public void getPraiseUserList(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        JSONObject jsonObject = new JSONObject();
        if (dynaInfoId == 0) {
            AppControllerStatic.printErrorMsg(response, "参数有误", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        Pager<User> pager = dynaService.getPraiseUserList(dynaInfoId, pageNo, pageSize);
        JSONArray arr = new JSONArray();
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (User u : pager.getResultList()) {
                JSONObject obj = new JSONObject();
                obj.put("userId", u.getUid());
                obj.put("nickName", u.getNickname());
                arr.add(obj);
            }
        }
        jsonObject.put("userList", arr);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, null, pager, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 1.1.31 判断是否已赞
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getHadPraiseList.do", method = RequestMethod.GET)
    public void getHadPraiseList(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String[] dynaInfoIds = WebUtils.param(request, "dynaInfoIds", "").split(",");
        long userId = AppControllerStatic.paramUserId(request);
        JSONObject jsonObject = new JSONObject();
        if (userId == 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数userId", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        List<Long> ids = new ArrayList<Long>();
        for (String id : dynaInfoIds) {
            try {
                ids.add(Long.parseLong(id));
            } catch (NumberFormatException e) {
                continue;
            }
        }
        if (ids.isEmpty()) {
            AppControllerStatic.printErrorMsg(response, "非法参数dynaInfoIds", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        List<DynaPraise> list = dynaService.listDynaPraise(userId, ids);
        String idString = "";
        for (DynaPraise r : list) {
            idString = idString + r.getDynainfoId() + ",";
        }

        String[] idStrings = {};
        if (!StringUtils.isBlank(idString)) {
            idString = idString.substring(0, idString.length() - 1);
            idStrings = idString.split(",");
        }
        //字符串数组转Long型数组
        Long[] idLongs = new Long[idStrings.length];
        for (int i = 0; i < idStrings.length; i++) {
            idLongs[i] = Long.parseLong(idStrings[i]);
        }

        jsonObject.put("hadPraiseList", idLongs);
        AppControllerStatic.printSuccessMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 1.1.32 未读已赞数
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getUnReadCount.do", method = RequestMethod.GET)
    public void getUnReadCount(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取用户登录信息
        User user = userAuth.getUser(request, response);
        JSONObject jsonObject = new JSONObject();
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "用户末登录", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //未读数
        jsonObject.put("praiseNotReadCount", dynaService.getUnReadCount(user.getUid()));
        //获取最新的赞我的用户昵称，时间
        List<DynaPraise> unReadList = dynaService.getUnReadList(user.getUid());
        if (!unReadList.isEmpty() && unReadList.size() > 0) {
            DynaPraise dynaPraise = unReadList.get(0);
            if (dynaPraise != null) {
                User praiseUser = userService.findUser(dynaPraise.getUserId());
                jsonObject.put("latestPraiseMsg", praiseUser == null ? "" : praiseUser.getNickname());
                jsonObject.put("latestPraiseTime", dynaPraise.getPraiseTime());
            }
        }

        AppControllerStatic.printSuccessMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 获取同城动态最新发表时间
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/getTongChengDynaDateTime.do", method = RequestMethod.GET)
    public void getTongChengDynaDateTime(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);

        long provinceId = AppControllerStatic.paramProvinceId(request);
        long cityId = AppControllerStatic.paramCityId(request);

        String callback = AppControllerStatic.paramCallback(request);

        JSONObject jsonObject = new JSONObject();

        if (provinceId == 0) {
            AppControllerStatic.printErrorMsg(response, "非法参数provinceId", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        //同城动态列表,(只返回说说，帖子，提问帖子）
        List<Integer> typeList = new ArrayList<Integer>();
        typeList.add(DynaInfo.TYPE_TOPIC);
        typeList.add(DynaInfo.TYPE_COMMENT);
        typeList.add(DynaInfo.TYPE_QUESTION);
        Pager<DynaInfo> pager = dynaService.getDynaListByCityIdDynaType(provinceId, cityId, typeList, 1, 1);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                jsonObject.put("latestDateTime", dynaInfo.getCreateDate());
            }
        } else {
            jsonObject.put("latestDateTime", "");
        }

        //返回Json
        AppControllerStatic.printSuccessMsg(response, jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     *
     * 动态置顶
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/setDynaTop.do", method = RequestMethod.POST)
    public void setDynaTop(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取相关参数
        String callback = AppControllerStatic.paramCallback(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);

        JSONObject jsonObject = new JSONObject();

        //权限判断
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "您还没有登陆", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        ClubMember clubMember = memberService.findClubMemberByUserId(user.getUid());
        if (clubMember == null || !clubMember.isAdministrator()) {
            AppControllerStatic.printErrorMsg(response, "没有权限", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        long clubId = clubMember.getClubId();
        ClubDyna clubDyna = dynaService.findClubDynaById(dynaInfoId, clubId);
        if (clubDyna == null) {
            AppControllerStatic.printErrorMsg(response, "动态不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        if (clubDyna.getIsTop() == ClubDyna.TOP_YES) {
            AppControllerStatic.printErrorMsg(response, "动态已置顶", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        if (dynaService.getTopCount(clubId) >= MAX_TOP_COUNT) {
            AppControllerStatic.printErrorMsg(response, "置顶最多只能显示5条，请先删除！", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        boolean isSuccess = dynaService.doTop(dynaInfoId, clubId);
        if (isSuccess) {
            AppControllerStatic.printSuccessMsg(response, jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        } else {
            AppControllerStatic.printErrorMsg(response, "置顶失败", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        }
    }

    /**
     *
     * 取消置顶
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/cancelDynaTop.do", method = RequestMethod.POST)
    public void cancelDynaTop(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取相关参数
        String callback = AppControllerStatic.paramCallback(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);

        JSONObject jsonObject = new JSONObject();

        //权限判断
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "您还没有登陆", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        ClubMember clubMember = memberService.findClubMemberByUserId(user.getUid());
        if (clubMember == null || !clubMember.isAdministrator()) {
            AppControllerStatic.printErrorMsg(response, "没有权限", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        long clubId = clubMember.getClubId();
        ClubDyna clubDyna = dynaService.findClubDynaById(dynaInfoId, clubId);
        if (clubDyna == null) {
            AppControllerStatic.printErrorMsg(response, "动态不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        boolean isSuccess = dynaService.cancelTop(dynaInfoId, clubId);
        if (isSuccess) {
            AppControllerStatic.printSuccessMsg(response, jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        } else {
            AppControllerStatic.printErrorMsg(response, "置顶失败", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        }
    }

    /**
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/dynaTopList.do", method = RequestMethod.GET)
    public void dynaTopList(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取相关参数
        String callback = AppControllerStatic.paramCallback(request);
        long clubId = AppControllerStatic.paramClubId(request);

        JSONObject jsonObject = new JSONObject();
        JSONArray arr = new JSONArray();

        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "车友会不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        Pager<DynaInfo> pager = dynaService.getTopDynaListByClubId(clubId, MAX_TOP_COUNT);
        if (pager != null && !pager.getResultList().isEmpty()) {
            List<DynaInfo> list = pager.getResultList();
            for (DynaInfo dynaInfo : list) {
                //品牌车友会活动动态屏蔽
                if (dynaInfo.getType() == DynaInfo.TYPE_JOIN_ACTIVITY) {
                    continue;
                }
                String content = dynaInfo.getTitle() == null ? "" : dynaInfo.getTitle().trim();
                int dynaType = dynaInfo.getType();
                if (dynaType == DynaInfo.TYPE_ACTIVITY) {
                    Activity activity = activityService.findById(dynaInfo.getTargetId());
                    content = activity == null ? "" : activity.getTitle();
                } else if (dynaType == DynaInfo.TYPE_PICTURE) {
                    Album album = albumService.getAlbumById(dynaInfo.getTargetId());
                    User u = userService.findUser(dynaInfo.getCreateBy());
                    content = u.getNickname() + "上传了" + dynaInfo.getPictureNum() + "张相片到《" + album.getName() + "》";
                } else if (dynaType == DynaInfo.TYPE_COMMENT) {
                    if (StringUtils.isBlank(dynaInfo.getContent())) {
                        User user = userService.findUser(dynaInfo.getCreateBy());
                        content = user == null ? "" : user.getNickname() + "发表了一条说说";
                    } else {
                        content = dynaInfo.getContent();
                    }
                    content = dynaService.updateDynaContentByImages(dynaInfo.getDynaInfoId(), content);
                } else if (dynaType == DynaInfo.TYPE_VOTE) {
                    Vote vote = voteService.findVoteByVoteId(dynaInfo.getTargetId());
                    content = vote.getVoteTitle();
                } else if (dynaType == DynaInfo.TYPE_NOTICE) {
                    content = dynaInfo.getContent();
                } else if (dynaType == DynaInfo.TYPE_CREATE_CLUB) {
                    content = "车友会主页：" + systemConfig.getRoot() + "/" + club.getClubUrl() + "/，大家快去邀请车友加入吧！";
                } else if (dynaType == DynaInfo.TYPE_JOIN_CLUB) {
                    content = "我已经加入了" + club.getName() + "，现在我是一个有组织的人了！";
                }
                dynaInfo.setTitle(content);
                JSONObject obj = new JSONObject();
                obj.put("dynaInfoId", dynaInfo.getDynaInfoId());
                obj.put("dynaType", dynaType);
                obj.put("content", dynaInfo.getTitle());
                obj.put("dynaTargetId", dynaInfo.getTargetId());
                obj.put("pubTime", dynaInfo.getCreateDate());
                obj.put("createBy", dynaInfo.getCreateBy());
                arr.add(obj);
            }
        }

        jsonObject.put("dynaTopList", arr);

        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 帖子列表（发布时间倒序）
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/topic/topicListByCreateTime.do", method = RequestMethod.GET)
    public void getTopicListByCreateTime(HttpServletRequest request, HttpServletResponse response) {
        getTopicListByClubIdOrder(request, response, ORDER_CREATE_DATE);

    }

    /**
     * 帖子列表（回复时间倒序）
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/topic/topicListByPostTime.do", method = RequestMethod.GET)
    public void getTopicListByPostTime(HttpServletRequest request, HttpServletResponse response) {
        getTopicListByClubIdOrder(request, response, ORDER_POST_DATE);
    }

    /**
     * 根据车友会ID和order查找帖子列表
     *
     * @param request
     * @param response
     * @param order
     */
    private void getTopicListByClubIdOrder(HttpServletRequest request, HttpServletResponse response, int order) {
        WebUtils.setEncoding4App(request, response);
        long clubId = AppControllerStatic.paramClubId(request);
        String callback = AppControllerStatic.paramCallback(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);

        JSONObject jsonObject = new JSONObject();

        //参数检查          
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", jsonObject, callback, AppControllerStatic.V1_VERSION);
            return;
        }

        //设置返回值  
        JSONArray jsonTopicArray = new JSONArray();
        Pager<DynaInfo> pager = dynaService.getDynaListByClubIdOrder(clubId, pageNo, pageSize, order, DynaInfo.TYPE_TOPIC, DynaInfo.TYPE_QUESTION);
        if (pager != null && !pager.getResultList().isEmpty()) {
            for (DynaInfo dynaInfo : pager.getResultList()) {
                JSONObject jsonContent = new JSONObject();
                JSONObject jsonAuthorInfo = new JSONObject();
                if (dynaInfo.getCreateBy() > 0) {
                    User user = userService.findUser(dynaInfo.getCreateBy());
                    if (user != null) {
                        jsonAuthorInfo.put("realName", user.getTruename());
                        jsonAuthorInfo.put("nickName", user.getNickname());
                        jsonAuthorInfo.put("userId", user.getUid());
                    }
                }
                jsonContent.put("author", jsonAuthorInfo);
                jsonContent.put("createAt", dynaInfo.getCreateDate());
                jsonContent.put("isPic", dynaInfo.getIsPic());
                jsonContent.put("isPick", dynaInfo.getPick());
                jsonContent.put("isRecommend", dynaInfo.getRecommend());
                jsonContent.put("lastPostAt", dynaInfo.getLastPostAt());
                jsonContent.put("replyCount", dynaInfo.getPostCount());
                jsonContent.put("title", xssEncode == 0
                        ? dynaInfo.getTitle() : EncodeUtils.encodeForHTMLFilterNull(dynaInfo.getTitle()));
                jsonContent.put("topicId", dynaInfo.getTopicId());
                jsonContent.put("url", systemConfig.getBbsRoot() + "/topic-" + dynaInfo.getTopicId() + ".html");

                jsonTopicArray.add(jsonContent);
            }
        }
        jsonObject.put("topicList", jsonTopicArray);
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V1_VERSION);
    }

    /**
     * 删除动态
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/deleteDyna.do", method = RequestMethod.POST)
    public void deleteDyna(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);

        JSONObject jsonObject = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        //判断是否有操作权限
        ClubMember clubMember = memberService.findClubMemberByUserId(user.getUid());
        if (clubMember != null && !clubMember.isAdmin() && !clubMember.isViceAdmin()) {
            AppControllerStatic.printErrorMsg(response, "没有该操作权限", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        //判断动态是否存在
        ClubDyna clubDyna = dynaService.findClubDynaById(dynaInfoId, clubMember.getClubId());
        if (clubDyna == null) {
            AppControllerStatic.printErrorMsg(response, "该动态不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        DynaInfo info = dynaService.findDynaInfoById(clubDyna.getDynaInfoId(), clubDyna.getCreateDate());
        if (clubDyna.getType() == DynaInfo.TYPE_ACTIVITY) {//活动动态
            dynaService.deleteUserDyna(clubDyna.getCreateBy(), dynaInfoId);//活动终端都删除了，其他也要删除，不然有脏数据
            activityService.removeActivity(info.getTargetId());//删除活动
            dynaIndexService.deleteDynaIndex( dynaInfoId );  //删除动态索引
        } else if (clubDyna.getType() == DynaInfo.TYPE_VOTE) {//投票动态
            dynaService.deleteUserDyna(clubDyna.getCreateBy(), dynaInfoId);
            voteService.deleteVote(info.getTargetId());
            dynaIndexService.deleteDynaIndex( dynaInfoId );  //删除动态索引
        }

        boolean isSuccess = dynaService.deleteClubDyna(clubMember.getClubId(), dynaInfoId) > 0;
        if (isSuccess) {
            //DynaInfo info = dynaService.findDynaInfoById(clubDyna.getDynaInfoId(), clubDyna.getCreateDate());
            if (((clubDyna.getType() == DynaInfo.TYPE_TOPIC)
                    || (clubDyna.getType() == DynaInfo.TYPE_QUESTION))
                    && info != null) {
                //帖子被会长删除，通知用户
                messageService.send(userService.findUser(info.getCreateBy()), MessageService.DELETE_TOPIC,
                        info.getTitle(), messageService.assembleUserUrl(user.getUid(), user.getNickname()), user.getNickname());
            }
            //说说被会长删除，通知用户
            if (clubDyna.getType() == DynaInfo.TYPE_COMMENT) {
                messageService.send(userService.findUser(info.getCreateBy()), MessageService.DELETE_COMMENT,
                        messageService.assembleUserUrl(user.getUid(), user.getNickname()), user.getNickname());
            }
            AppControllerStatic.printSuccessMsg(response, jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        } else {
            AppControllerStatic.printErrorMsg(response, "删除失败", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        }

    }

    /**
     * 从动态详情页面删除动态（只有创建者自己才有权限可以删除） 删除个人动态，删除车友会动态，删除同城动态，删除说说动态
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/dyna/deleteDynaFromDetail.do", method = RequestMethod.POST)
    public void deleteDynaFromDetail(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        String callback = AppControllerStatic.paramCallback(request);
        long dynaInfoId = AppControllerStatic.paramDynaInfoId(request);

        JSONObject jsonObject = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        //判断动态是否存在
        UserDyna userDyna = dynaService.findUserDynaById(dynaInfoId, user.getUid());
        if (userDyna == null) {
            AppControllerStatic.printErrorMsg(response, "没有操作权限", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        DynaInfo info = dynaService.findDynaInfoById(userDyna.getDynaInfoId(), userDyna.getCreateDate());
        if (userDyna.getType() == DynaInfo.TYPE_VOTE) {//投票动态
            voteService.deleteVote(info.getTargetId());
        }
        dynaService.deleteUserDyna(userDyna.getUserId(), dynaInfoId);
        dynaService.deleteCityDyna(info.getDynaInfoId(), info.getProvinceId(), info.getCityId());
        dynaService.deleteClubDyna(info.getClubId(), dynaInfoId);
        dynaIndexService.deleteDynaIndex( dynaInfoId ); //删除动态索引
        for(long id:info.getSubjectList()){
        	 Subject subject = subjectService.findSubjectById(id);
        	 SubjectDyna subjectDyna = subjectDynaService.findSubjectDynaById(id, dynaInfoId);
             if(subjectDyna != null){
             	   //删除话题动态同时减去话题相应的讨论数    话题的讨论数 = 话题动态数+话题动态回复数 
             	  subject.setReplyCount(subject.getReplyCount()-subjectDyna.getPostCount());
             }
        	//删除话题中的说说
            long deleteId = subjectDynaService.deleteSubjectDyna(id, dynaInfoId);
            if(deleteId == 0) {
            	continue;
            }
           
            //话题的评论数-1
            if(subject != null) {
            	subject.setReplyCount(subject.getReplyCount() - 1);
            	geliDao.update(subject, "reply_count");
            }
        }
        AppControllerStatic.printSuccessMsg(response, jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
    }
     /**
     * 1.4.2 点赞/取消赞
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/2.0/dyna/doPraise.do", method = RequestMethod.POST)
    public void doPraise2(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        List<Long> praiseDynaInfoIds = WebUtils.paramLongs(request, "praiseDynaInfoIds");
        List<Long> unPraiseDynaInfoIds = WebUtils.paramLongs(request, "unPraiseDynaInfoIds");
        JSONObject jsonObject = new JSONObject();
        //获取用户登录信息
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() == 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        if (praiseDynaInfoIds == null && unPraiseDynaInfoIds == null) {
            AppControllerStatic.printErrorMsg(response, "参数有误", jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        JSONArray failPraiseIds = new JSONArray();
        if (praiseDynaInfoIds != null) {
            for (long praiseDynaInfoId: praiseDynaInfoIds) {
                DynaIndex dynaIndex = dynaIndexService.findDynaIndex(praiseDynaInfoId);
                if(dynaIndex !=null && dynaIndex.getStatus() == DynaIndex.STATUS_NORMAL){
                    try{
                        if (!dynaService.doPraise(user.getUid(),praiseDynaInfoId,dynaIndex.getCreateDate(),DynaPraise.STATUS_PRAISE)) {
                            failPraiseIds.add(praiseDynaInfoId);
                        }
                    }catch(Exception ex){
                        log.error("doPraise is fail id={}",praiseDynaInfoId);
                    }
                }else{
                    failPraiseIds.add(praiseDynaInfoId);
                }
            }
        }
        JSONArray failNotPraiseIds = new JSONArray();
        if (unPraiseDynaInfoIds != null) {
            for (long unPraiseDynaInfoId :  unPraiseDynaInfoIds) {
                DynaIndex dynaIndex = dynaIndexService.findDynaIndex(unPraiseDynaInfoId);
                if(dynaIndex!=null && dynaIndex.getStatus() == DynaIndex.STATUS_NORMAL){
                    try{
                        if (!dynaService.doPraise(user.getUid(), dynaIndex.getDynaInfoId(),dynaIndex.getCreateDate(), DynaPraise.STATUS_UNPRAISE)) {
                            failNotPraiseIds.add(unPraiseDynaInfoId);
                        }
                    }catch(Exception ex){
                        log.error("unPraise is fail id={}",unPraiseDynaInfoId);
                        failNotPraiseIds.add(unPraiseDynaInfoId);
                    }
                }else{
                    failNotPraiseIds.add(unPraiseDynaInfoId);
                }
            }
        }
        jsonObject.put("time", System.currentTimeMillis());
        if (failPraiseIds.isEmpty() && failNotPraiseIds.isEmpty()) {
            AppControllerStatic.printSuccessMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
        } else {
            jsonObject.put("failPraiseDynaInfoIds", failPraiseIds);
            jsonObject.put("failUnPraiseDynaInfoIds", failNotPraiseIds);
            AppControllerStatic.printErrorMsg(response, jsonObject, null, AppControllerStatic.V1_VERSION_LATEST);
        }
    }
}
